$(function() {
    var success = function(res) {
        layui.layer.msg(res.msg);
        if (res.code == 1) {
            location.reload();
        }
    };
    //关闭窗口
    layform.on('submit(menuedit-new)', function(data) {
        _ajax(null, data.field, function(res) {
            layer.msg(res.msg);
            if (res.code) {
                layer_iframe_close(333, true);
            }
        });
        return false;
    });
    //图片上传
    $('.new-style-add-img').change(function() {
        var new_img_box = $(this).parent('.new-style-file').next('.new-style-file-box');
        
        var img_length = new_img_box.data('length');
        var type = new_img_box.data('type');
        var file_key = new_img_box.data('file_key');
        var this_width = parseFloat(new_img_box.data('width'));
      ;
        var is_show_input = new_img_box.data('input');
        if(!file_key){
            file_key="image[]";
        }
      
        if (new_img_box.children().length >= img_length && img_length !== '') {
            layui.layer.msg("只能上传" + img_length + "张图片");
            return;
        }
        var file = $(this).get(0).files[0];
        var reader = new FileReader();
        reader.readAsDataURL(file);
        reader.onload = function(e) {
            var path = e.target.result;
            var img_div = '<div class="new-style-img-box fl">';
            if(this_width > 0){
          
                var img = new Image();
                img .src = path;
                img.onload = function(){

                    var that = this;
                    var w = that.width,
                    h = that.height,
                    scale = w / h;
                    var max_width = w > h?w:h;
                    if(max_width > this_width){
                       
                        if(w > h){
                            w = this_width;
                            h = this_width/scale;
                        }else{
                            h = this_width;
                            w = this_width/scale;
                        }
                    
                        var canvas = document.createElement('canvas');
                        var ctx = canvas.getContext('2d');
                        var anw = document.createAttribute("width");
                        anw.nodeValue = w;
                        var anh = document.createAttribute("height");
                        anh.nodeValue = h;
                        canvas.setAttributeNode(anw);
                        canvas.setAttributeNode(anh);

                        ctx.drawImage(that, 0, 0, w, h);
                        path = canvas.toDataURL('image/jpeg');
                        img_div += '<img src="' + path + '"><span class="new-style-img-close" data-type="1"><img src="/image/close.png"></span><input type="hidden" name="'+file_key+'" value="' + path + '">';
                        if(is_show_input > 0){
                            img_div += '<input type="text" name="new_name[]" class="new-style-input" placeholder="请输入图片名称">';
                        }
                        img_div += '<input type="hidden" name="new_type[]" value="'+type+'"></div>';
                        new_img_box.append(img_div);
                        
                       
                    } else{
                            img_div += '<img src="' + path + '"><span class="new-style-img-close" data-type="1"><img src="/image/close.png"></span><input type="hidden" name="'+file_key+'" value="' + path + '">';
                            if(is_show_input > 0){
                                img_div += '<input type="text" name="new_name[]" class="new-style-input" placeholder="请输入图片名称">';
                            }
                            img_div += '<input type="hidden" name="new_type[]" value="'+type+'"></div>';
                            new_img_box.append(img_div);
                    }
                }
            }else{
                img_div += '<img src="' + path + '"><span class="new-style-img-close" data-type="1"><img src="/image/close.png"></span><input type="hidden" name="'+file_key+'" value="' + path + '">';
                if(is_show_input > 0){
                    img_div += '<input type="text" name="new_name[]" class="new-style-input" placeholder="请输入图片名称">';
                }
                img_div += '<input type="hidden" name="new_type[]" value="'+type+'"></div>';
                new_img_box.append(img_div);
            }
          
            //new_img_box.append('<div class="new-style-img-box fl"><img src="' + path + '"><span class="new-style-img-close" data-type="1"><img src="/image/close.png"></span><input type="hidden" name="'+file_key+'" value="' + path + '"><input type="text" name="new_name[]" class="new-style-input" placeholder="请输入图片名称"><input type="hidden" name="new_type[]" value="'+type+'"></div>');
        }
    })
    //删除图片
    $('.new-style-file-box').on('click', '.new-style-img-close', function() {
        var type = $(this).data('type');
        if (type == 1) {
            $(this).parent('.new-style-img-box').remove();
            layui.layer.msg('删除成功');
            return;
        }
        var that = $(this);
        var path = $(this).data('path');
        var id = $(this).data('id');
        var other = $(this).data('other');
        $.post('delImg', {
            id: id,
            path: path,
            other:other  //其他参数
        }, function(res) {
            layui.layer.msg(res.msg);
 
            if (res.code == 0) {
                that.parent('.new-style-img-box').remove();
            }
        })
    })
});
/**
 * 充值from表单
 */
function resetFrom() {
    $('#head-form').children().val('');
}
/**
 * 省市县三级联动
 */
layui.use('element', function() {
    var form = layui.form;
    form.on('select(pub)', function(data) {
        var _this_id = data.elem.id;
        var _this_index = '';
        var url = "/broker/broker/getChild";
        var param = {};
        var n_name = '';
        var n_value = '';
        param.name = data.value;
        switch (_this_id) {
            case 'new-province':
                _this_index = 0;
                param.grade = 1;
                n_name  = n_value = 'city_name';
                break;
            case 'new-city':
                _this_index = 1;
                param.grade = 2;
                n_name  = n_value  = 'counties_name';
                break;
            case 'new-comm':
                _this_index = 2;
                param.grade = 3;
                n_name = 'address',n_value = 'id';
                break;
            case 'new-add':
                _this_index = 3;
                param.grade = 4;
                n_name = 'number',n_value = 'id';
                break;
            case 'new-broker':
                _this_index = 4;
                break;
        }
        _init_dom(_this_index);
        form.render();
        getAjaxData(url, param, function(obj) {
            if (obj.data) {
                for (i = 0; i < obj.data.area.length; i++) {
                    $('select[lay-filter=\'pub\']').eq(_this_index + 1).append('<option value="' + obj.data.area[i][n_value] + '">' + obj.data.area[i][n_name] + '</option>');
                }
                form.render();
            }
        })
    });
});

function getAjaxData(url, data, callback) {
    $.ajax({
        url: url, //请求的url地址
        dataType: "json", //返回的格式为json
        async: true, //请求是否异步，默认true异步，这是ajax的特性
        data: data, //参数值
        type: "POST", //请求的方式
        success: callback
    })
}
/**
 * 省市县三级联动dom 初始化
 * @param now_index 当前选中dom下标
 * @private
 */
function _init_dom(now_index) {
    if(now_index <= 3){
        if ($('#new-broker').length > 0) {
            $('#new-broker').empty();
            $('#new-broker').append('<option value="">请选择经纪人</option>');
        }
    }
    if (now_index <= 2) {
        if ($('#new-add').length > 0) {
            $('#new-add').empty();
            $('#new-add').append('<option value="">请选择区域</option>');
        }
    }
    if (now_index <= 1) {
        if ($('#new-comm').length > 0) {
            $('#new-comm').empty();
            $('#new-comm').append('<option value="">请选择县/区</option>');
        }
    }
    if (now_index == 0) {
        if ($('#new-city').length > 0) {
            $('#new-city').empty();
            $('#new-city').append('<option value="">请选择市</option>');
        }
    }
    if (now_index != 4) {
        if ($('#new-broker').length > 0) {
            $('#new-broker').empty();
            $('#new-broker').append('<option value="">请选择经纪人</option>');
        }
    }
}